package com.orangelife.service.propFeeMeterRecord.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeMeterRecord.IPropFeeMeterRecordService;
import com.orangelife.service.propFeeMeterRecord.model.PropFeeMeterRecordDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeMeterRecord类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-07-22 14:58:19
 * ==============================================================
 */
@Service("propFeeMeterRecordService")
public class PropFeeMeterRecordServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeMeterRecordService {
	private static Logger logger = Logger
			.getLogger(PropFeeMeterRecordServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeMeterRecordServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeMeterRecord(PropFeeMeterRecordDto propFeeMeterRecord)
			throws AppBizException {

		Assert.notNull(propFeeMeterRecord);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeMeterRecord.getRecord())) {
			if (propFeeMeterRecord.getRecord().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "读数长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getPeriod())) {
			if (propFeeMeterRecord.getPeriod().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "期数长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getUseAmount())) {
			if (propFeeMeterRecord.getUseAmount().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "使用量长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getStatus())) {
			if (propFeeMeterRecord.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"状态(0,本期，1上期，2上上期,3往期)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getVer())) {
			if (propFeeMeterRecord.getVer().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "版本长度超出");
			}
		}

		if (propFeeMeterRecord.getVer() == null) {

			propFeeMeterRecord.setVer("0");
		}

		propFeeMeterRecord.setRecordID(IdUtil.getuuId());
		propFeeMeterRecord.setItemStat(TypeConst.FLAG_ON);
		propFeeMeterRecord.setTscreate(new Date());
		propFeeMeterRecord.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeMeterRecordMapper.insertSelective", propFeeMeterRecord);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeMeterRecord(PropFeeMeterRecordDto propFeeMeterRecord)
			throws AppBizException {

		Assert.notNull(propFeeMeterRecord);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeMeterRecord.getRecordID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeMeterRecordDto oldPropFeeMeterRecord = this
				.findPropFeeMeterRecordByID(propFeeMeterRecord.getRecordID());

		if (!StringUtils.isBlank(propFeeMeterRecord.getRecord())) {
			if (propFeeMeterRecord.getRecord().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "读数长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getPeriod())) {
			if (propFeeMeterRecord.getPeriod().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "期数长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getUseAmount())) {
			if (propFeeMeterRecord.getUseAmount().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "使用量长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getStatus())) {
			if (propFeeMeterRecord.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"状态(0,本期，1上期，2上上期,3往期)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getVer())) {
			if (propFeeMeterRecord.getVer().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "版本长度超出");
			}
		}

		if (oldPropFeeMeterRecord == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeMeterRecord.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeMeterRecordMapper.updateByPrimaryKeySelective",
				propFeeMeterRecord);
	}

	@Override
	public PropFeeMeterRecordDto findPropFeeMeterRecordByID(
			String propFeeMeterRecordID) throws AppBizException {
		if (StringUtils.isBlank(propFeeMeterRecordID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeMeterRecordDto oldPropFeeMeterRecord = this.getSqlSession()
				.selectOne("PropFeeMeterRecordSearchMapper.selectByPrimaryKey",
						propFeeMeterRecordID);
		if (oldPropFeeMeterRecord == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeMeterRecord.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeMeterRecord;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeMeterRecordByLogic(String propFeeMeterRecordID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeMeterRecordID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeMeterRecordDto oldPropFeeMeterRecord = this
				.findPropFeeMeterRecordByID(propFeeMeterRecordID);

		oldPropFeeMeterRecord.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeMeterRecord.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeMeterRecordMapper.updateByPrimaryKeySelective",
				oldPropFeeMeterRecord);
	}

	@Override
	public List<Map<String, Object>> findPropFeeMeterRecordByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterRecordSearchMapper.findAllPropFeeMeterRecord",
						map);
		return list;
	}

	@Override
	public List<PropFeeMeterRecordDto> getPropFeeMeterRecordByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeMeterRecordDto> list = this.getSqlSession().selectList(
				"PropFeeMeterRecordSearchMapper.getAllPropFeeMeterRecordDto",
				map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeMeterRecordByRelevantID(
			String recordID, String meterID, String status)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("recordID", recordID);
		map.put("meterID", meterID);
		map.put("status", status);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterRecordSearchMapper.selectPropFeeMeterRecordDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeMeterRecordDto> findPropFeeMeterRecordByRelevantID(
			String recordID, String meterID, String status, String record,
			String period, String useAmount, String ver) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("recordID", recordID);
		map.put("meterID", meterID);
		map.put("status", status);
		map.put("record", record);
		map.put("period", period);
		map.put("useAmount", useAmount);
		map.put("ver", ver);

		List<PropFeeMeterRecordDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterRecordSearchMapper.findPropFeeMeterRecordDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeMeterRecordDto> bauthfindPropFeeMeterRecord(
			List<String> propFeeMeterRecordIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeMeterRecordDto> list = this.getSqlSession().selectList(
				"PropFeeMeterRecordSearchMapper.batchFindPropFeeMeterRecord",
				propFeeMeterRecordIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeMeterRecordByMap(
			Map<String, Object> propFeeMeterRecord) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterRecordSearchMapper.batchFindPropFeeMeterRecordByMap",
						propFeeMeterRecord);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	/***
	 * 批量初始化初始化读数
	 */
	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeMeterRecord(
			List<PropFeeMeterRecordDto> propFeeMeterRecords)
			throws AppBizException {

		List<PropFeeMeterRecordDto> feeMeterRecords = new ArrayList<PropFeeMeterRecordDto>();
		for (int i = 0; i < propFeeMeterRecords.size(); i++) {

			PropFeeMeterRecordDto propFeeMeterRecord = propFeeMeterRecords
					.get(i);

			Assert.notNull(propFeeMeterRecord);
			/************************************************
			 * 判断输入参数的长度以及是否能为空
			 ************************************************/

			if (!StringUtils.isBlank(propFeeMeterRecord.getRecord())) {
				if (propFeeMeterRecord.getRecord().length() > 36) {
					throw new AppBizException(CodeConst.SYS_CODE_1002, "读数长度超出");
				}
			}

			if (!StringUtils.isBlank(propFeeMeterRecord.getPeriod())) {
				if (propFeeMeterRecord.getPeriod().length() > 32) {
					throw new AppBizException(CodeConst.SYS_CODE_1002, "期数长度超出");
				}
			}

			if (!StringUtils.isBlank(propFeeMeterRecord.getUseAmount())) {
				if (propFeeMeterRecord.getUseAmount().length() > 32) {
					throw new AppBizException(CodeConst.SYS_CODE_1002,
							"使用量长度超出");
				}
			}

			if (!StringUtils.isBlank(propFeeMeterRecord.getStatus())) {
				if (propFeeMeterRecord.getStatus().length() > 1) {
					throw new AppBizException(CodeConst.SYS_CODE_1002,
							"状态(0,本期，1上期，2上上期,3往期)长度超出");
				}
			}

			if (!StringUtils.isBlank(propFeeMeterRecord.getVer())) {
				if (propFeeMeterRecord.getVer().length() > 16) {
					throw new AppBizException(CodeConst.SYS_CODE_1002, "版本长度超出");
				}
			}
			propFeeMeterRecord.setRecordID(IdUtil.getuuId());
			propFeeMeterRecord.setItemStat(TypeConst.FLAG_ON);
			propFeeMeterRecord.setTscreate(new Date());
			propFeeMeterRecord.setTsedit(new Date());
			propFeeMeterRecord.setVer("0");
			propFeeMeterRecord.setStatus("1");
			propFeeMeterRecord.setRecord(StringUtils.toString(propFeeMeterRecord.getRecord(), null));
			propFeeMeterRecord.setUseAmount(StringUtils.toString(propFeeMeterRecord.getUseAmount(), null));
			/***
			 * 清除已经存在的记录
			 */
			List<PropFeeMeterRecordDto> list = this
					.findPropFeeMeterRecordByRelevantID(null,
							propFeeMeterRecord.getMeterID(), null, null, null,
							null, propFeeMeterRecord.getVer());

			if (list == null || list.size() <= 0) {
				feeMeterRecords.add(propFeeMeterRecord);
			}
			if (list != null && list.size() > 0) {
				list.get(0).setRecord(propFeeMeterRecord.getRecord());
				list.get(0).setUseAmount(propFeeMeterRecord.getUseAmount());
				list.get(0).setPeriod(propFeeMeterRecord.getPeriod());
				
				list.get(0).setVer("0");
				this.editPropFeeMeterRecord(list.get(0));
				// propFeeMeterRecords.remove(i);
			}

		}
		if (feeMeterRecords != null && feeMeterRecords.size() > 0) {
			return this.getSqlSession().insert(
					"PropFeeMeterRecordBauthMapper.bauthInsert",
					feeMeterRecords);
		}
		return 1;

	}

	@Override
	public List<Map<String, Object>> checkRecordIsExist(
			Map<String, Object> propFeeMeterRecord) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeMeterRecordSearchMapper.checkRecordIsExist",
				propFeeMeterRecord);
		return list;
	}

	@Override
	public int editPropFeeMeterRecordFull(
			PropFeeMeterRecordDto propFeeMeterRecord) throws AppBizException {

		Assert.notNull(propFeeMeterRecord);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeMeterRecord.getRecordID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeMeterRecordDto oldPropFeeMeterRecord = this
				.findPropFeeMeterRecordByID(propFeeMeterRecord.getRecordID());

		if (!StringUtils.isBlank(propFeeMeterRecord.getRecord())) {
			if (propFeeMeterRecord.getRecord().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "读数长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getPeriod())) {
			if (propFeeMeterRecord.getPeriod().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "期数长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getUseAmount())) {
			if (propFeeMeterRecord.getUseAmount().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "使用量长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getStatus())) {
			if (propFeeMeterRecord.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"状态(0,本期，1上期，2上上期,3往期)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getVer())) {
			if (propFeeMeterRecord.getVer().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "版本长度超出");
			}
		}

		if (oldPropFeeMeterRecord == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeMeterRecord.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeMeterRecordMapper.updateByPrimaryKeySelective2",
				propFeeMeterRecord);
	}
	@Override
	public int editPropFeeMeterRecordAndMoney(
			PropFeeMeterRecordDto propFeeMeterRecord) throws AppBizException {

		Assert.notNull(propFeeMeterRecord);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeMeterRecord.getRecordID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeMeterRecordDto oldPropFeeMeterRecord = this
				.findPropFeeMeterRecordByID(propFeeMeterRecord.getRecordID());

		if (!StringUtils.isBlank(propFeeMeterRecord.getRecord())) {
			if (propFeeMeterRecord.getRecord().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "读数长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getPeriod())) {
			if (propFeeMeterRecord.getPeriod().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "期数长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getUseAmount())) {
			if (propFeeMeterRecord.getUseAmount().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "使用量长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getStatus())) {
			if (propFeeMeterRecord.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"状态(0,本期，1上期，2上上期,3往期)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecord.getVer())) {
			if (propFeeMeterRecord.getVer().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "版本长度超出");
			}
		}

		if (oldPropFeeMeterRecord == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeMeterRecord.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeMeterRecordMapper.updateMoneyByPrimaryKeySelective",
				propFeeMeterRecord);
	}
}